package com.zhuoshi.controller;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.stream.events.Namespace;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhuoshi.bean.WdHtBuild;
import com.zhuoshi.bean.WdHtProject;
import com.zhuoshi.bean.WdHtUnit;
import com.zhuoshi.service.WdHtBuildService;
import com.zhuoshi.service.WdHtProjectService;
import com.zhuoshi.service.WdHtUnitService;
import com.zhuoshi.serviceImpl.WdHtBuildServiceImpl;

import net.sf.json.JSONArray;

@Controller
public class WdHtUnitController {

	@Autowired
	private WdHtUnitService WdHtUnitServiceImpl;
	/**
	 *  查询全部的单元
	 */
	@RequestMapping("/unit/list")
	public String list(Model model)
	{
		List<WdHtUnit> list = WdHtUnitServiceImpl.getAll("");
		model.addAttribute("list", list);
		return "unit_list";
	}
	
	/**
	 * 跳转到添加界面
	 */
	@RequestMapping("/unit/toAdd")
	public String toAdd(Model model) {
		List<WdHtProject> list = WdHtUnitServiceImpl.getAllProject();
		model.addAttribute("list", list);
		return "unit_add";
	} 
	
	/**
	 *  获取楼栋信息
	 * @throws IOException 
	 */
	@RequestMapping("/unit/getBuild")
	public void getBuild(Model model,String projectId,HttpServletResponse hsp) throws IOException {
		
		List<WdHtBuild> list = WdHtUnitServiceImpl.getAllBuild(projectId);
		
		JSONArray fromObject = JSONArray.fromObject(list);
		
		hsp.getWriter().print(fromObject.toString());
	}
	
	 /**
	 * 添加一个的功能
	 */
	 @RequestMapping("/unit/add")
	 public String add(WdHtUnit wdHtUnit) {
	 //设置uuid
	 wdHtUnit.setUnitId((UUID.randomUUID().toString()));
	 WdHtUnitServiceImpl.add(wdHtUnit);
	 return "redirect:/unit/list.do";
	 }
	 /**
	 * 删除一个的功能
	 */
	 @RequestMapping("/unit/del")
	 public String del(String unitId)
	 {
	 //删除的service方法
	  WdHtUnitServiceImpl.del(unitId);
	  return "redirect:/unit/list.do";
	 }
	 
	 /** 
	 * 去添加的路上
	 */
	 @RequestMapping("/unit/toupdate")
	 public String toUp(String unitId,Model model)
	 {
		 //查询全部的项目信息 
		 List<WdHtProject> list = WdHtUnitServiceImpl.getAllProject();
		 model.addAttribute("list", list);
		 //查询一个
		  WdHtUnit bean = WdHtUnitServiceImpl.selectOne(unitId);
		  model.addAttribute("bean",bean);
		  return "unit_update";
	 }
	
	 /**
	 * 更新一个的功能
	 */
	 @RequestMapping("/unit/update")
	 public String toUp(WdHtUnit wdHtUnit)
	 {
		 System.out.println(wdHtUnit.getBuildId()+"=="+wdHtUnit.getProjectId());
	     //更新一个
		 WdHtUnitServiceImpl.upOne(wdHtUnit);
	     return "redirect:/unit/list.do";
	 }
	 
	 
	    /**
		 *  查询全部的单元通过楼栋id
	     * @throws IOException 
		 */
		@RequestMapping("/unit/unitList")
		public void listByBuildId(Model model,String buildId,HttpServletResponse hsp) throws IOException
		{
			List<WdHtUnit> list = WdHtUnitServiceImpl.getAll(buildId);
			JSONArray fromObject = JSONArray.fromObject(list);
			hsp.getWriter().print(fromObject.toString());
		}
}
